home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / INTERPP.ICN < prev    next >
Text File  |  1992-09-28  |  8KB  |  379 lines

  1. ############################################################################
  2. #
  3. #    File:     interpp.icn
  4. #
  5. #    Subject:  Program to interpret Icon programs
  6. #
  7. #    Author:   Jerry Nowlin
  8. #
  9. #    Date:     December 30, 1991
  10. #
  11. ###########################################################################
  12. #  
  13. #    This program is kind of like an interactive version of BASIC in that Icon
  14. #  expressions are entered with line numbers and you can resequence them list
  15. #  them etc. and execute all the lines entered.  There is no editor built
  16. #  in.  You have to retype a line to change it.
  17. #
  18. #    Documentation is lacking but there is a "?" help command that lists all
  19. #  the other commands.
  20. #
  21. ############################################################################
  22. #
  23. #  See also:  interpe.icn
  24. #
  25. ############################################################################
  26.  
  27. global    WHITE,    # the white space cset
  28.     MFLAG,    # the modified flag
  29.     PRTBL    # the program table
  30.  
  31. procedure main(arg)
  32.    local line, lno, pline
  33.  
  34. #    define the needed cset
  35.     WHITE := ' \t\n\f'
  36.  
  37. #    initialize the program table
  38.     PRTBL := table()
  39.  
  40. #    initialize the modified flag
  41.     MFLAG := 0
  42.  
  43. #    get all the input
  44.     writes("Icon> ")
  45.     while line := read() do {
  46.  
  47. #        scan the input line
  48.         line ? {
  49.  
  50. #            skip any initial white space
  51.             tab(many(WHITE))
  52.  
  53. #            check for program lines (they have line numbers)
  54.             if lno := tab(many(&digits)) & tab(many(WHITE)) then {
  55.  
  56. #                get the program line
  57.                 pline := tab(0)
  58.  
  59. #                store the line in the program table
  60.                 PRTBL[numeric(lno)] := pline
  61.  
  62. #                set the modified flag
  63.                 MFLAG +:= 1
  64.             }
  65.  
  66. #            read command
  67.             else if (tab(upto(WHITE)) | tab(0)) ==
  68.                 ("read" | "r") then {
  69.                 readprog()
  70.  
  71. #                clear the modified flag
  72.                 MFLAG := 0
  73.             }
  74.  
  75. #            write command
  76.             else if (tab(upto(WHITE)) | tab(0)) ==
  77.                 ("write" | "w") then {
  78.                 writeprog()
  79.  
  80. #                clear the modified flag
  81.                 MFLAG := 0
  82.             }
  83.  
  84. #            delete command
  85.             else if (tab(upto(WHITE)) | tab(0)) ==
  86.                 ("delete" | "d") then {
  87.                 delprog()
  88.  
  89. #                set the modified flag
  90.                 MFLAG +:= 1
  91.             }
  92.  
  93. #            sequence command
  94.             else if (tab(upto(WHITE)) | tab(0)) ==
  95.                 ("sequence" | "s") then {
  96.                 seqprog()
  97.             }
  98.  
  99. #            list command
  100.             else if (tab(upto(WHITE)) | tab(0)) ==
  101.                 ("list" | "l") then {
  102.                 listprog()
  103.             }
  104.  
  105. #            execute command
  106.             else if (tab(upto(WHITE)) | tab(0)) ==
  107.                 ("execute" | "e") then {
  108.                 execprog()
  109.             }
  110.  
  111. #            help command
  112.             else if (tab(upto(WHITE)) | tab(0)) ==
  113.                 ("help" | "h" | "?") then {
  114.                 helpprog()
  115.             }
  116.  
  117. #            quit command
  118.             else if (tab(upto(WHITE)) | tab(0)) ==
  119.                 ("quit" | "q") then {
  120.                 quitprog()
  121.             }
  122.  
  123. #            invalid syntax input
  124.             else {
  125.                 write("Syntax Error: ",line)
  126.                 helpprog()
  127.             }
  128.         }
  129.         writes("Icon> ")
  130.     }
  131.  
  132. end
  133.  
  134. procedure execprog()
  135.    local runargs, out, prog, line, command
  136.  
  137.     static    tmpfile
  138.  
  139.     initial tmpfile := "TMPFILE.icn"
  140.  
  141. #    get any runtime arguments
  142.     runargs := tab(0)
  143.  
  144. #    create the temporary Icon file
  145.     (out := open(tmpfile,"w")) |
  146.  
  147. #    or mention the problem and fail
  148.     (write("I can't open '",tmpfile,"' for writing") & fail)
  149.  
  150. #    sort the program table
  151.     prog := sort(PRTBL)
  152.  
  153. #    put the program in the file
  154.     every line := !prog do {
  155.         write(out,line[2])
  156.     }
  157.     close(out)
  158.  
  159. #    format the command to execute the program
  160.     command := "icont -s " || tmpfile || " -x " || runargs
  161.  
  162. #    add the command to remove the temporary file
  163.     command ||:= " ; rm -f " || tmpfile
  164.  
  165. #    execute the command
  166.     system(command)
  167.  
  168. end
  169.  
  170. procedure seqprog()
  171.    local begno, incno, prog, lno, l
  172.  
  173. #    initialize the sequencing numbers
  174.     begno := incno := 10
  175.  
  176. #    skip any white space
  177.     tab(many(WHITE))
  178.  
  179. #    get an initial line number
  180.     begno := numeric(tab(many(&digits)))
  181.  
  182. #    skip any white space
  183.     tab(many(WHITE))
  184.  
  185. #    get a increment number
  186.     incno := numeric(tab(many(&digits)))
  187.  
  188. #    sort the program table
  189.     prog := sort(PRTBL)
  190.  
  191. #    reinitialize it
  192.     PRTBL := table()
  193.  
  194. #    sequence the program lines starting with begno by incno
  195.     lno := begno
  196.     every l := !prog do {
  197.         PRTBL[lno] := l[2]
  198.         lno +:= incno
  199.     }
  200.  
  201. end
  202.  
  203. procedure readprog()
  204.    local readfile, response, in, lno, line
  205.  
  206. #    get a possible command line file name
  207.     tab(many(WHITE))
  208.     readfile := tab(upto(WHITE) | 0)
  209.  
  210. #    if there was no file with the command get one
  211.     if /readfile | *readfile = 0 then {
  212.         writes("Read file name: ")
  213.         readfile := read()
  214.     }
  215.  
  216. #    make sure a modified file has been written
  217.     if MFLAG > 0 then {
  218.         writes("Write before reading over current program? ")
  219.         response := read()
  220.         if any('yY',response) then
  221.             writeprog()
  222.     }
  223.  
  224. #    initialize the program table
  225.     PRTBL := table()
  226.  
  227. #    read the program from the read file
  228.     in := open(readfile,"r")
  229.     lno := 10
  230.     every line := !in do {
  231.         PRTBL[lno] := line
  232.         lno +:= 10
  233.     }
  234.     close(in)
  235.  
  236. #    tell them what you did
  237.     write("Read '",readfile,"'...",*PRTBL," lines")
  238.  
  239. end
  240.  
  241. procedure writeprog()
  242.    local writefile, prog, out, l
  243.  
  244. #    get a possible command line file name
  245.     tab(many(WHITE))
  246.     writefile := tab(upto(WHITE) | 0)
  247.  
  248. #    if there was no file with the command get one
  249.     if /writefile | *writefile = 0 then {
  250.         writes("Write file name: ")
  251.         writefile := read()
  252.     }
  253.  
  254. #    sort the program table
  255.     prog := sort(PRTBL)
  256.  
  257. #    write the program to the write file
  258.     out := open(writefile,"w")
  259.     every l := !prog do {
  260.         write(out,l[2])
  261.     }
  262.     close(out)
  263.  
  264. #    tell them what you did
  265.     write("Write '",writefile,"'...",*PRTBL," lines")
  266.  
  267. end
  268.  
  269. procedure delprog()
  270.    local begno, endno, prog, l, lno
  271.  
  272. #    initialize the line numbers
  273.     begno := 0
  274.     endno := 99999
  275.  
  276. #    skip any white space
  277.     tab(many(WHITE))
  278.  
  279. #    get an initial line number
  280.     begno := endno := numeric(tab(many(&digits)))
  281.  
  282. #    skip any white space
  283.     tab(many(WHITE))
  284.  
  285. #    get a final line number
  286.     endno := numeric(tab(many(&digits)))
  287.  
  288. #    sort the program table
  289.     prog := sort(PRTBL)
  290.  
  291. #    reinitialize it
  292.     PRTBL := table()
  293.  
  294. #    delete the program lines between the optional numbers
  295.     every l := !prog do {
  296.         lno := numeric(l[1])
  297.         if (lno < begno) | (lno > endno) then PRTBL[lno] := l[2]
  298.     }
  299.  
  300. end
  301.  
  302. procedure listprog()
  303.    local begno, endno, prog, l, lno
  304.  
  305. #    initialize the line numbers
  306.     begno := 0
  307.     endno := 99999
  308.  
  309. #    skip any white space
  310.     tab(many(WHITE))
  311.  
  312. #    get an initial line number
  313.     begno := endno := numeric(tab(many(&digits)))
  314.  
  315. #    skip any white space
  316.     tab(many(WHITE))
  317.  
  318. #    get a final line number
  319.     endno := numeric(tab(many(&digits)))
  320.  
  321. #    sort the program table
  322.     prog := sort(PRTBL)
  323.  
  324. #    list the program lines between the optional numbers
  325.     every l := !prog do {
  326.         lno := numeric(l[1])
  327.         if (lno >= begno) & (lno <= endno) then
  328.             write(right(lno,5),": ",l[2])
  329.         if lno > endno then break
  330.     }
  331.  
  332. end
  333.  
  334. procedure helpprog()
  335.  
  336.     static helpmsg
  337.  
  338. #    define the help message
  339.     initial {
  340.         helpmsg := [
  341.         "<<< Icon Expression Syntax >>>",
  342.         "",
  343.         "lineno expression",
  344.         "",
  345.         "<<< Command Summary >>>",
  346.         " (1st character works)",
  347.         "",
  348.         "read [ file ]",
  349.         "write [ file ]",
  350.         "list [ begno [ endno ] ]",
  351.         "delete [ begno [ endno ] ]",
  352.         "sequence [ begno [ increment ] ]",
  353.         "execute [ args ]",
  354.         "quit",
  355.         "help"
  356.         ]
  357.     }
  358.  
  359. #    print it
  360.     every write(!helpmsg)
  361.  
  362. end
  363.  
  364. procedure quitprog()
  365.    local response
  366.  
  367. #    make sure a modified file has been written
  368.     if MFLAG > 0 then {
  369.         writes("Write before quitting? ")
  370.         response := read()
  371.         if any('yY',response) then
  372.             writeprog()
  373.     }
  374.  
  375.     stop("Goodbye.")
  376.  
  377. end
  378.  
  379.